//格式化日期
const formatTime = date => {
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()

	return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

//时间戳转换为时间格式
const timestampDate = (time, type) => {
	var date = new Date(time);
	var YY = date.getFullYear();
	var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
	var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
	var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
	var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
	var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
	// return YY + MM + DD + " " + hh + mm + ss;
	var arr = [
		YY + "-" + MM + "-" + DD,
		YY + "-" + MM + "-" + DD + " " + hh + ":" + mm + ":" + ss,
		YY + "-" + MM + "-" + DD + " " + hh + ":" + mm,
		YY + "年" + MM + "月" + DD,
		YY + "年" + MM + "月" + DD + " " + hh + ":" + mm + ":" + ss,
		hh + ":" + mm + ":" + ss,
		MM + "-" + DD
	]
	return arr[type];
}

//格式化时间格式
const formatDate = (date, type) => {
	var YY = date.getFullYear();
	var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
	var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
	var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
	var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
	var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
	// return YY + MM + DD + " " + hh + mm + ss;
	var arr = [
		YY + "-" + MM + "-" + DD,
		YY + "-" + MM + "-" + DD + " " + hh + ":" + mm + ":" + ss,
		YY + "-" + MM + "-" + DD + " " + hh + ":" + mm,
		YY + "年" + MM + "月" + DD,
		YY + "年" + MM + "月" + DD + " " + hh + ":" + mm + ":" + ss,
		hh + ":" + mm + ":" + ss,
		MM + "-" + DD,
		hh + ":" + mm,
		'' + YY + MM + DD,
	]
	return arr[type];
}

/**
 * 获取指定日期
 * @param {*} type 年月日周  0-1-2-3
 * @param {*} symbol 正负：表示时间加减
 * @param {*} sum 时间加减的量
 * @param {*} fmtype 输出时间类型
 */
function getTheDay(type, symbol, sum, fmtype) {
	//创建date变量
	var nowDate = new Date();
	if (type == 2) {
		nowDate.setDate(nowDate.getDate() + sum * symbol);
	} else if (type == 3) {
		nowDate.setDate(nowDate.getDate() + 7 * sum * symbol);
	} else if (type == 1) {
		nowDate.setMonth(nowDate.getMonth() + sum * symbol);
	} else if (type == 0) {
		nowDate.setYear(nowDate.getFullYear() + sum * symbol);
	}
	return formatDate(nowDate, fmtype);
}

//获取当前年月日
function getDay(str) {
	var y = new Date().getFullYear(); //获取年份
	var m = new Date().getMonth() + 1; //获取月份
	var d = new Date().getDate(); //获取日
	m = m < 10 ? '0' + m : m; //月数补 0
	d = d < 10 ? '0' + d : d; //日数补 0
	return [y, m, d].join(str)
}

//获取当前月份的第一天
function getFirstDay(str) {
	var y = new Date().getFullYear(); //获取年份
	var m = new Date().getMonth() + 1; //获取月份
	var d = '01'
	m = m < 10 ? '0' + m : m; //月份补 0
	return [y, m, d].join(str)
}

//获取当前月份的最后一天
function getLastDay(str) {
	var y = new Date().getFullYear(); //获取年份
	var m = new Date().getMonth() + 1; //获取月份
	var d = new Date(y, m, 0).getDate(); //获取当月最后一日
	m = m < 10 ? '0' + m : m; //月份补 0
	d = d < 10 ? '0' + d : d; //日数补 0
	return [y, m, d].join(str)
}

//比较时间大小
function compareDate(date2, date1, days) {
	// 1.转为时间戳
	var data1 = Date.parse(date1.replace(/-/g, "/"));
	var data2 = Date.parse(date2.replace(/-/g, "/"));
	// 2.时间戳间隔 再加上一天
	var datadiff = data2 - data1 + 86400000;
	// 3.七天间隔
	var time = days * 24 * 60 * 60 * 1000;
	// 4.比较两个日期的远近，date2为最近的日期
	if (data1 > data2) {
		return 1; //时间不能小于当前时间
	} else {
		// 当间隔日期大于 规定的7天
		if (datadiff > time) {
			return 2; //超过指定时间
		} else {
			return 0
		}
	}
}

//单位数赋0:1月份=>01月份
const formatNumber = n => {
	n = n.toString()
	return n[1] ? n : `0${n}`
}

//日期+时间
const years = [];
const months = [];
const days = [];
const hours = [];
const minutes = [];

//获取年
for (let i = 2020; i <= 2050; i++) {
	years.push("" + i);
}

//获取月份
for (let i = 1; i <= 12; i++) {
	if (i < 10) {
		i = "0" + i;
	}
	months.push("" + i);
}

//获取日期
for (let i = 1; i <= 31; i++) {
	if (i < 10) {
		i = "0" + i;
	}
	days.push("" + i);
}

//获取小时
for (let i = 0; i < 24; i++) {
	if (i < 10) {
		i = "0" + i;
	}
	hours.push("" + i);
}

//获取分钟
for (let i = 0; i < 60; i++) {
	if (i < 10) {
		i = "0" + i;
	}
	minutes.push("" + i);
}

module.exports = {
	formatTime,
	timestampDate,
	formatDate,
	getTheDay,
	getDay,
	getFirstDay,
	getLastDay,
	compareDate,
	formatNumber
}